#pragma once

#include <iostream>
#include <vector>
#include <easyx.h>
#include <graphics.h>
#include <string>

using namespace std;

class Draw
{
private:
    int width = 0, height = 0;
    int TextColor = RED, LineColor = GREEN, CoordColor = BLUE, BackColor = WHITE;
    int R = 5;
public:
    void Start(int w, int h)
    {
        width = w, height = h;
        initgraph(width, height, EW_SHOWCONSOLE);
        setbkcolor(BackColor);
        setfillcolor(CoordColor);
        setlinecolor(LineColor);
        setbkmode(TRANSPARENT);
        settextcolor(TextColor);
        cleardevice();
    }

    void DrawLine(int x1, int y1, int x2, int y2, int med = 0)
    {
        int line_color;
        switch (med)
        {
        case 1:
            line_color = RED;
            break;
        case 2:
            line_color = BLACK;
            break;
        case 3:
        case 4:
            line_color = RGB(255, 128, 192);
            break;
        default:
            line_color = LineColor;
            break;
        }
        setlinecolor(line_color);
        line(x1, y1, x2, y2);

        double distance = sqrt((y1 - y2) * (y1 - y2) + (x1 - x2) * (x1 - x2));
        double tmpx = double(x1 + (x2 - x1) * (1 - (12 * sqrt(3) / 2) / distance));
        double tmpy = double(y1 + (y2 - y1) * (1 - (12 * sqrt(3) / 2) / distance));
        if (y1 == y2)
        {
            line(x2, y2, int(tmpx), int(tmpy + 6));
            line(x2, y2, int(tmpx), int(tmpy - 6));
        }
        else
        {
            double k = (double(x2) - double(x1)) / (double(y1) - double(y2));
            double increX = 6 / sqrt(k * k + 1);
            double increY = 6 * k / sqrt(k * k + 1);
            line(x2, y2, int(tmpx + increX), int(tmpy + increY));
            line(x2, y2, int(tmpx - increX), int(tmpy - increY));
        }
        setlinecolor(LineColor);
    }

    void DrawCoord(int x, int y)
    {
        fillcircle(x, y, R);
    }

    void DrawWord(int x, int y, const string& txt)
    {
        outtextxy(x, y, txt.c_str());
    }

    void OpenPhoto(string plase)
    {
        IMAGE img;
        loadimage(&img, plase.c_str(), width, height);
        putimage(0, 0, &img);
    }

    void Clear()
    {
        cleardevice();
    }


    ~Draw()
    {
        closegraph();
    }
};